package com.dofast.bt.mvp.presenter.login;

import android.text.TextUtils;
import android.util.Log;

import com.dofast.bt.account.Account;
import com.dofast.bt.account.AccountManager;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.LoginBean;

import com.dofast.bt.bean.ShopBean;
import com.dofast.bt.mvp.model.CallBack;
import com.dofast.bt.mvp.model.login.IUserModel;
import com.dofast.bt.mvp.model.login.UserModel;
import com.dofast.bt.mvp.view.activity.login.LoginView;

import com.dofast.bt.mvp.view.activity.main.MainActivity;
import com.google.gson.Gson;

import org.xutils.ex.DbException;

import java.util.HashMap;
import java.util.Map;


/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：登录界面
 */

public class LoginPresenter extends BaseMvpPresenter<LoginView> implements ILoginPresenter {
    private IUserModel userModel;//model层具体实现类

    public LoginPresenter() {
        this.userModel = new UserModel();
    }

    @Override
    public void login() {
        checkViewAttach();//检查是否绑定
        //检查登录信息是否为空
        checkLogin();
        mvpView.showLoading("正在登录....");
        Map map = new HashMap<String, String>();
        map.put(LoginBean.USERNAME, mvpView.getUsername());//测试用"GJXC0077"
        map.put(LoginBean.PASSWORD, mvpView.getPassword());//"测试用"000000"
        BaseApplication.getLogger().e(new Gson().toJson(map));

        userModel.login(map, new CallBack<Account>() {
            @Override
            public void onSuccess(Account loginBean, boolean isSuccess, String msg) {
                mvpView.hideLoading();
                if (isSuccess) {
                    loginBean.setPwd(mvpView.getPassword());
                    AccountManager.getInstance().login(loginBean);
                    //// TODO: 2016/10/14 0014 跳转界面
                    mvpView.gotoActivity(MainActivity.class, true);

                    mvpView.showResult("登录成功");

                } else {
                    mvpView.showResult(msg);

                }
            }

            @Override
            public void onFailure(String fail) {
                mvpView.hideLoading();
                BaseApplication.getLogger().e("info", fail);
                mvpView.showResult("网络不给力咯");

            }
        });

    }


    @Override
    public void autoLogin() {
        if (null != BaseApplication.getInstance().getAccount()) {
            mvpView.autoLogin(BaseApplication.getInstance().getAccount().getJobNo(), BaseApplication.getInstance().getAccount().getPwd());
        }
    }


    private void checkLogin() {
        ShopBean shopBean = new ShopBean();
        try {
            BaseApplication.getDbManager().saveBindingId(shopBean);


        Log.d("DB=-----------------",new Gson().toJson(BaseApplication.getDbManager().findAll(ShopBean.class)));

        } catch (DbException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(mvpView.getUsername()) || TextUtils.isEmpty(mvpView.getPassword())) {
            mvpView.showErr("用户名或者密码不能为空");
            mvpView.hideLoading();
            return;
        }
    }
}
